#include "shared_library.h"

#ifdef WINDOWS_OS
#include <Windows.h>
#else
#include <dlfcn.h>
#include <errno.h>
#include <stdio.h>
#endif

shared_library::shared_library(const char* library)
{

#ifdef WINDOWS_OS
	if ((handle_ = ::LoadLibrary(library)) == NULL)
#else
	if ((handle_ = dlopen(library, RTLD_LAZY|RTLD_LOCAL)) == NULL)
#endif
	{
		throw ("Failed to load library.");		
	}
}

shared_library::~shared_library()
{
	if(!handle_)
		return;
#ifdef WINDOWS_OS
	::FreeLibrary((HMODULE)handle_);
#else
	dlclose(handle_);
#endif /* WIN32 */
}

void* shared_library::address(const char* symbol_name) const
{
	if (NULL == symbol_name)
	{
		return NULL;
	}
#ifdef WINDOWS_OS
	return (void*)::GetProcAddress((HMODULE)handle_,symbol_name);
#else
	return (void*)dlsym(handle_, symbol_name);
#endif
}
